package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.CodeLoginContract;

import java.util.HashMap;
import java.util.Map;

import static com.sdcqjy.mylibrary.http.HttpClient.HttpType.JSON;
import static com.sdcqjy.property.presenter.contract.a.CodeLogin;
import static com.sdcqjy.property.presenter.contract.a.SmsCode;

/**
 * Created by 磁磁帅 on 2018/5/5.
 * 功能：
 */

public class CodeLoginPresenter extends BaseContract.Presenter<CodeLoginContract.View> {
    public CodeLoginPresenter(CodeLoginContract.View baseView) {
        super(baseView);
    }

    public void sendMsm(String tel) {
        new HttpClient()
                .addParam("telePhone", tel)
                .setHttpType(JSON).httpRequest(SmsCode, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.sendMsmRet(ret);
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void login(String tel, String code) {
        //{"telePhone":"1888888888889","smsCode":"1562"}
        Map<String, Object> param = new HashMap<>();
        param.put("telePhone", tel);
        param.put("smsCode", code);
        new HttpClient()
                .setParam(param)
                .setHttpType(JSON).httpRequest(CodeLogin, new BaseRequestBack<BaseRet<LoginMode>>() {
            public BaseRet<LoginMode> jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, new TypeToken<BaseRet<LoginMode>>() {
                }.getType());
            }

            public void onResponseSucc(BaseRet<LoginMode> ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.loginSucc(ret.data);
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }
}
